package com.sky.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.ShoppingCart;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShopCartMapper;
import com.sky.service.ShopCartService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShopCartServiceImpl implements ShopCartService {
    @Autowired
    private ShopCartMapper shopCartMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 查看购物车
     *
     * @return
     */
    @Override
    public List<ShoppingCart> findShopCart() {
        List<ShoppingCart> shoppingCarts = shopCartMapper.findShopCart(BaseContext.getCurrentId());
        return shoppingCarts;
    }

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    @Override
    public void addShopCart(ShoppingCartDTO shoppingCartDTO) {
        //判断为不为空
        if (ObjectUtil.isNull(shoppingCartDTO)) {
            return;
        }
        ShoppingCart shopCart = new ShoppingCart();
        //拷贝数据到shopCart
        BeanUtils.copyProperties(shoppingCartDTO, shopCart);
        shopCart.setUserId(BaseContext.getCurrentId());
        //动态查找当前购物车的商品
        List<ShoppingCart> shoppingCarts = shopCartMapper.list(shopCart);
        //判断当前加入到购物车中的数据是否已经存在
        if (ObjectUtil.isNotNull(shoppingCarts) && !shoppingCarts.isEmpty()) {
            //存在,则修改原来的数据库中的数据number+1
            shopCart.setNumber(1);
            shopCartMapper.updateShopCart(shopCart);
        } else {
            //不存在,则加入到数据库中
            //判断购物车加入的是菜品还是套餐数据
            Long dishId = shoppingCartDTO.getDishId();
            shopCart.setNumber(1);
            shopCart.setCreateTime(LocalDateTime.now());
            if (dishId != null) {
                //dishId不为空则是加入的是菜品
                Dish dish = dishMapper.findById(Math.toIntExact(dishId));
                shopCart.setAmount(dish.getPrice());
                shopCart.setName(dish.getName());
                shopCart.setImage(dish.getImage());
            } else {
                //dishId为空则加入的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.findById(Math.toIntExact(setmealId));
                shopCart.setAmount(setmeal.getPrice());
                shopCart.setName(setmeal.getName());
                shopCart.setImage(setmeal.getImage());
            }
            shopCartMapper.addShopCart(shopCart);
        }

    }

    /**
     * 删除购物车中一个商品
     *
     * @param shoppingCartDTO
     */
    @Override
    public void deleteById(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询当前购物车中对应商品的数量
        List<ShoppingCart> list = shopCartMapper.list(shoppingCart);
        ShoppingCart cart = list.get(0);
        //判断商品中的数量是否大于1
        if (cart.getNumber() > 1) {
            //数量大于1则修改当前商品数量-1
            shoppingCart.setNumber(-1);
            shopCartMapper.updateShopCart(shoppingCart);
        } else {
            //数量等于1则删除
            shopCartMapper.deleteById(shoppingCart);
        }

    }

    /**
     * 清空购物车
     */
    @Override
    public void deleteAll() {
        shopCartMapper.deleteAll(BaseContext.getCurrentId());
    }
}
